﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Hawaii.Services.Client;
using Hawaii.Services.Client.Relay;

namespace PhoneApp1
{
    public partial class PhonePage1 : PhoneApplicationPage
    {
        public PhonePage1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets the relay data context.
        /// </summary>
        private RelayContext RelayContext
        {
            get { return ((App)App.Current).RelayContext; }
        }

        /// <summary>
        /// Button handler for creating a group
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Creategroup_Click(object sender, RoutedEventArgs e)
        {
            if (this.Name_Textbox.Text == "")
            {
                this.DisplayMessage("You must enter a group name", "Error");
            }
            else
            {
                //RelayService.CreateGroupAsync(HawaiiClient.HawaiiApplicationId,
                                       //       this.OnCompleteCreateGroup);
            }
        }

        /// <summary>
        ///  Call back routine for completion of joining  a group
        /// </summary>
        /// <param name="result"></param>
        private void OnCompleteCreateGroup(GroupResult result)
        {
            Debug.Assert(result != null, "result is null");

            if (result.Status == Status.Success)
            {
                // add the newly created group to the list of groups for this Relay Context
                this.RelayContext.Groups.Add(result.Group);

                this.Dispatcher.BeginInvoke(
                    delegate
                    {
                        //this.textBlock2.Text = "New Group : " + result.Group.RegistrationId;
                    });
            }
            else
            {
                // Display a message box, in case of any error creating a new endpoint.
                this.DisplayMessage("Error creating app group.", "Error");
            }

        }

        /// <summary>
        /// Helper method to display messages.
        /// </summary>
        /// <param name="message">A valid message.</param>
        /// <param name="caption">A valid caption.</param>
        private void DisplayMessage(string message, string caption)
        {
            this.Dispatcher.BeginInvoke(
                    delegate
                    {
                        MessageBox.Show(message, caption, MessageBoxButton.OK);
                    });
        }
    }
}
